﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.Article.Business;
using gdlmo.Common;

namespace gdlmo.DNN.Modules.Article
{
	public abstract class ArticleEdit : PortalModuleBase
	{

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{			
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Controls
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;		
		protected System.Web.UI.WebControls.DropDownList cboCategory;
		protected System.Web.UI.WebControls.RequiredFieldValidator valCategory;
		protected System.Web.UI.WebControls.TextBox txtTitle;
		protected System.Web.UI.WebControls.RequiredFieldValidator valTitle;
		protected System.Web.UI.WebControls.TextBox txtKeyWords;
		protected System.Web.UI.WebControls.TextBox txtAuthor;
		protected System.Web.UI.WebControls.DropDownList dlsDept;
		protected System.Web.UI.WebControls.TextBox txtCreatedDate;
		protected System.Web.UI.HtmlControls.HtmlInputFile flContent;
		protected System.Web.UI.HtmlControls.HtmlTable tbArticleEdit;
		#endregion

		protected System.Web.UI.WebControls.TextBox txtSummary;
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		protected System.Web.UI.HtmlControls.HtmlInputFile File2;
		protected System.Web.UI.HtmlControls.HtmlInputFile File3;
		protected System.Web.UI.HtmlControls.HtmlInputFile File4;
		protected System.Web.UI.WebControls.RequiredFieldValidator valSummary;
		protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;

		#region Private Members
		private int ArticleID;
		#endregion
		
		#region Private Function
		protected void BinderCategories()
		{			
			CategoryController objCtlCategory = new CategoryController();
			cboCategory.DataTextField = "Name";
			cboCategory.DataValueField = "CategoryId";
			cboCategory.DataSource = objCtlCategory.GetByModules(ModuleId);
			cboCategory.DataBind();
		}
		protected void BinderDepts()
		{			
			dlsDept.Items.Clear();
			dlsDept.DataTextField = "Name";
			dlsDept.DataValueField = "Name";

			dlsDept.DataSource = CommonFunc.GetDepts();
			dlsDept.DataBind();
		}

		#endregion

		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				ArticleController objCtlArticle = new ArticleController();
				ArticleInfo objArticle = new ArticleInfo();
				txtCreatedDate.Text = DateTime.Now.ToString("yyy-MM-dd");

				if (!(Request.Params["ArticleID"] == null)) 
				{
					ArticleID = Int32.Parse(Request.Params["ArticleID"]);					
				} 
				else 
				{
					ArticleID = Null.NullInteger;
				}
				if (!Page.IsPostBack) 
				{
					BinderCategories();
					BinderDepts();
					cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
					if (!Null.IsNull(ArticleID)) 
					{
						objArticle = objCtlArticle.Get(ArticleID);
						if (objArticle != null) 
						{							
							txtAuthor.Text = objArticle.Author;
							txtCreatedDate.Text = objArticle.CreatedDate.ToString();
							//txtKeyWords.Text = objArticle.
							cboCategory.SelectedValue = objArticle.CategoryId.ToString();
							CommonFunc.GetSelectValue(dlsDept.Items,objArticle.DeptName);		//当部门不再为活动时，添加到选择列表
							dlsDept.SelectedValue = objArticle.DeptName;
							txtTitle.Text = objArticle.Title;
							txtSummary.Text = objArticle.Summary;
							cmdDelete.Visible = true;
						} 
						else 
						{
							Response.Redirect(Globals.NavigateURL(), true);
						}
					}
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try 
			{
				if (Page.IsValid == true) 
				{
					ArticleInfo objArticle = new ArticleInfo();
					objArticle = ((ArticleInfo)CBO.InitializeObject(objArticle, typeof(ArticleInfo)));
					objArticle.ArticleID = ArticleID;					
					ArticleController objCtlArticle = new ArticleController();
					objArticle.CategoryId = Int32.Parse(cboCategory.SelectedValue);
					objArticle.Title = txtTitle.Text;
					objArticle.Author = txtAuthor.Text;
					objArticle.CreatedDate = DateTime.Now;
					objArticle.CreatedByUser = UserInfo.UserID.ToString();
//					objArticle.CreatedByUser = UserInfo.FullName; //UserId.ToString();
					objArticle.Summary = txtSummary.Text;
					objArticle.IP = Request.ServerVariables.Get("REMOTE_ADDR");
					objArticle.DeptName = dlsDept.SelectedValue.ToString();

					if (Null.IsNull(ArticleID)) 
					{
						objArticle.ArticleID = objCtlArticle.Add(objArticle,Request.Files);		//插入记录，并取得最新的编号
					} 
					else 
					{
						objCtlArticle.Update(objArticle,Request.Files);										
//						DotNetNuke.Services.Log.SiteLog.SiteLogInfo objInfoSiteLog = new DotNetNuke.Services.Log.SiteLog.SiteLogInfo();
						DotNetNuke.Services.Log.SiteLog.SiteLogController objCtlSiteLog = new DotNetNuke.Services.Log.SiteLog.SiteLogController();
						objCtlSiteLog.AddSiteLog(PortalId,UserId,Request.UrlReferrer.ToString(),Request.Url.ToString(),"修改文件",Request.UserHostAddress,Request.UserHostName,TabId,-1,1,"D");
					}
					Response.Redirect(Globals.NavigateURL(), true);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try 
			{
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try 
			{
				if (!Null.IsNull(ArticleID)) 
				{
					ArticleController objCtlArticle = new ArticleController();
					objCtlArticle.Delete(ArticleID);
//					DotNetNuke.Services.Log.EventLog.LogInfo objInfoLog = new DotNetNuke.Services.Log.EventLog.LogInfo();
//					objInfoLog.AddProperty(
//					DotNetNuke.Services.Log.EventLog.LogController objCtlLog = new DotNetNuke.Services.Log.EventLog.LogController();
//					objCtlLog.AddLog(
//					DotNetNuke.Services.Log.SiteLog.SiteLogInfo objInfoSiteLog = new DotNetNuke.Services.Log.SiteLog.SiteLogInfo();
					DotNetNuke.Services.Log.SiteLog.SiteLogController objCtlSiteLog = new DotNetNuke.Services.Log.SiteLog.SiteLogController();
//					objCtlSiteLog.AddSiteLog(PortalId,UserId,Request.UrlReferrer,Request.Url,Request.UserAgent,Request.UserHostAddress,Request.UserHostName,TabId,
//					objCtlSiteLog.AddSiteLog(PortalId,UserId,Request.UrlReferrer.ToString(),Request.Url.ToString(),Request.UserAgent,Request.UserHostAddress,Request.UserHostName,TabId,Int32.Parse(Request.QueryString["AffiliateId"].ToString()),1,"aaaa");
					objCtlSiteLog.AddSiteLog(PortalId,UserId,Request.UrlReferrer.ToString(),Request.Url.ToString(),"删除文件",Request.UserHostAddress,Request.UserHostName,TabId,-1,1,"D");
					
				}
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		#endregion

		
	}
}